package bookshelf.impl;

import bookshelf.Author;
import bookshelf.Book;
import bookshelf.BookShelf;

import java.util.*;

public class MyBookShelf implements BookShelf {
	
	private Map<Author, Set<Book>> bookTable = new HashMap<Author, Set<Book>>();

	@Override
	public void addBook(Book book, Author author) {
		if (!bookTable.containsKey(author))
			bookTable.put(author, new HashSet<Book>());
		bookTable.get(author).add(book);
	}

	@Override
	public Set<Book> getBooks() {
		final Set<Book> allBooks = new HashSet<Book>();
		for (final Set<Book> books : bookTable.values())
			allBooks.addAll(books);
		return allBooks;
	}

	@Override
	public Set<Author> getAuthors() {
		return Collections.unmodifiableSet(bookTable.keySet());
	}

	@Override
	public Set<Book> getBooks(Author author) {
        if (bookTable.get(author) == null)
            return Collections.emptySet();
		return Collections.unmodifiableSet(bookTable.get(author));
	}
}
